package com.ares.core.context;


import com.ares.basic.execptionHandler.ExceptionFactory;
import com.ares.session.XSession;
import org.springframework.http.HttpMethod;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Jintao on 2015/5/21.
 * <p/>
 * This class should be initialized in interceptor
 */
public class XContext {

    protected static final ThreadLocal<XContext> THREAD_LOCAL = ThreadLocal.withInitial(() -> new XContext());

    protected HttpServletRequest request;

    private XSession xSession = new XSession();

    /**
     * 请求开始时间
     */
    private long requestStartTime;

    /**
     * xcontext是否初始化
     */
    private boolean isInited = false;

    /**
     * 请求开源，openApi，wemall等
     */
    private String source;

    /**
     * 用户访问真实ip地址
     */
    private String realIp;

    protected XContext() {
    }

    /**
     * 获取当前Context
     *
     * @return
     */
    public static XContext getCurrentContext() {
        return THREAD_LOCAL.get();
    }

    public XSession getSession() {
        return this.xSession;
    }

    public void setSession(XSession xSession) {
        if (xSession != null) {
            this.xSession = xSession;
        }
    }

    public String getServletPath() {
        return this.request.getServletPath();
    }

    /**
     * 在Interceptor中使用，初始化参数
     */
    public void init(HttpServletRequest request) {
        this.request = request;
        this.setRequestStartTime(System.currentTimeMillis());
        this.isInited = true;
        this.realIp = request.getHeader("x-real-ip");
    }

    public boolean isInited() {
        return isInited;
    }

    public long getRequestStartTime() {
        return requestStartTime;
    }

    public void setRequestStartTime(long requestStartTime) {
        this.requestStartTime = requestStartTime;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getRealIp() {
        return realIp;
    }

    public void setRealIp(String realIp) {
        this.realIp = realIp;
    }

    /**
     * 判断请求类型是否为Post
     *
     * @return 是否为Post请求
     */
    public boolean isPostRequest() {
        if (request == null) {
            throw ExceptionFactory.create("F_WEBKITS_COMMON_1006");
        }

        boolean isPost = request.getMethod().equals(HttpMethod.POST.name()) || request.getMethod()
                .equals(HttpMethod.PUT.name());
        return isPost && isJsonRequest();
    }

    /**
     * 判断请求类型是否为Json
     *
     * @return 是否为Json请求
     */
    public boolean isJsonRequest() {
        if (request == null) {
            throw ExceptionFactory.create("F_WEBKITS_COMMON_1006");
        }
        String contentType = request.getContentType();
        if (contentType.contains("application/json") || contentType.contains("text/json")) {
            return true;
        }
        return false;
    }

    /**
     * 清理当前线程中的数据
     */
    public static void clear() {
        THREAD_LOCAL.remove();
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public String getUserId() {
        XSession xSession = THREAD_LOCAL.get().getSession();
        if (xSession == null) {
            return null;
        } else {
            return xSession.getUserId();
        }
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    public void setUserId(String userId) {
        XSession session = THREAD_LOCAL.get().getSession();
        if (session == null) {
            session = new XSession();
            session.setUserId(userId);
            THREAD_LOCAL.get().setSession(session);
        } else {
            session.setUserId(userId);
        }
    }

}